import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPool{
    private ArrayList<Thread> threads = new ArrayList<>();
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(10);


    public MyThreadPool(int n){
        for (int i = 0;i < n; i++){
            Thread t = new Thread(()->{
                while(true) {
                    try {
                       Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
            threads.add(t);
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
public class ThreadDemo4 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int n = i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行任务" + n + ",当前线程为：" + Thread.currentThread().getName());
                }
            });
        }
    }
}
